var gulp = require('gulp');
var browserSync = require('browser-sync');
var clean = require('gulp-clean');
var rev = require('gulp-rev');                                  //- 对文件名加MD5后缀
var revCollector = require('gulp-rev-collector');               //- 路径替换
var runSequence = require('gulp-run-sequence');
var yargs = require('yargs')
    .options({
        'w': {
            alias: 'watch',
            type: 'boolean'
        },
        's': {
            alias: 'server',
            type: 'boolean'
        },
        'p': {
            alias: 'port',
            type: 'number'
        },
        'r': {
            alias: 'rev',
            type: 'boolean'
        },
        'c': {
            alias: 'clean',
            type: 'boolean'
        }
    }).argv;
var path = {
	root: 'src',
	dist: 'webapp'
}
// 监听任务
gulp.task('watch', function () {
	// 监听所有资源文件
    gulp.watch(path.root +'/**/*.{css,js,png,jpg,gif,svg}', function(e){
    	gulp.src(e.path)
    	.pipe(browserSync.reload({stream: true}));
    });
    // 监听page下的html文件
    gulp.watch(path.root +'/page/*.html', function(e){
    	gulp.src(e.path)
    	.pipe(browserSync.reload({stream: true}));
    });
    // 监听modules
    gulp.watch(path.root +'/themes/modules/**/*.html', function(e){
    	gulp.src(path.root +'/page/index.html')
    	.pipe(browserSync.reload({stream: true}));
    });
});
//gulp.task('clean_rev', function(){
//	return gulp.src([path.root + '/config/*.js', '!' + path.root + '/config/core.js', '!' + path.root + '/config/menu.js', '!' + path.root + '/config/vp.config.js'])
//		.pipe(clean());
//})
gulp.task('rev', function(){
	return gulp.src(path.dist + '/config/{core,menu,vp.config}.js')
		.pipe(rev())
		.pipe(gulp.dest(path.dist + '/config/'))
		.pipe(rev.manifest())
		.pipe(gulp.dest(path.dist + '/rev'))
		.pipe(browserSync.reload({stream: true}));
})
gulp.task('clean_dist', function(){
	return gulp.src(path.dist)
		.pipe(clean());
})
gulp.task('copy', function(){
	return gulp.src(path.root + '/**/*')
		.pipe(gulp.dest(path.dist));
})
gulp.task('replace_rev', function(){
	return gulp.src([path.dist + '/rev/*.json', path.dist + '/**/*.{html,js,css}'])
		.pipe(revCollector({
			replaceReved: true,
		}))
		.pipe(gulp.dest(path.dist));
})
gulp.task('build', function(cb){
	if( yargs.r ){
		runSequence('clean_dist', 'copy', 'rev', 'replace_rev', cb);
	}else{
		runSequence('clean_dist', cb);
	}
})
// 参数说明
//  -w: 实时监听
//  -s: 启动服务器
//  -p: 服务器启动端口，默认8888
gulp.task('default',['build'], function () {
    if (yargs.s) {
        gulp.start('server');
    }

    if (yargs.w) {
        gulp.start('watch');
    }
});
// 创建本地服务
gulp.task('server', function () {
    yargs.p = yargs.p || 8000;
    browserSync.init({
        server: {
            baseDir: './',   		
            directory: true
        },
//      ui: {
//          port: yargs.p + 1,
//          weinre: {
//              port: yargs.p + 2
//          }
//      },
        open: true,
        port: yargs.p,
        startPath: path.root + '/page/login.html'												
    });
});


